<?php
namespace Home\Event;
//set_time_limit(100);
class WeixinEvent {
    public $access_token;
    public function __construct(){
        $this->access_token = accessToken();
        //占时用获取IP地址的方式更新过期access_token
        $this->getcallbackip();
    }

    /**
     * 获取服务器IP地址
     * @return mixed
     */
    public function getcallbackip(){
        $getUrl = "https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=".$this->access_token;
        $data = get_oneself($getUrl);
        if(isset($data['errcode']) && $data['errcode'] == 40001){
            $this->access_token = accessToken(true);
        }
        return $data;
    }

    /**
     * @param $access_token
     * $barcodeArr 数组
     * @return mixed
     * 生成带参二维码
     */
    public function twoDBarcode($barcodeArr){
        if(!is_array($barcodeArr)){
            //$this->error('参数错误');
            return false;
        }
        $getUrl = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$this->access_token;
        $params = array(
            'action_name' => "QR_LIMIT_STR_SCENE",
            'action_info' => array('scene' => array('scene_str' => $barcodeArr['b_parameter'])),
        );
        $params = json_encode($params);
        $data = post_oneself($getUrl,$params);
        //dump($data);die;
        return $data;
    }

    /**
     * @param $access_token
     * $barcodeArr 数组
     * @return mixed
     * 生成带参临时二维码
     */
    public function tmpBarcode($barcodeArr){
        if(empty($barcodeArr)){
            return false;
        }
        $getUrl = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$this->access_token;
        $params = array(
            'action_name' => "QR_SCENE",
            'action_info' => array('scene' => array('scene_id' => $barcodeArr)),
            'expire_seconds'=>2592000,//2592000  (30天)
        );
        $params = json_encode($params);
        $data = post_oneself($getUrl,$params);
        //dump($data);die;
        return $data;
    }



    /**
     * 获取消息模板ID
     * @param $template_id_short 例：OPENTM406445392
     * @return bool|mixed
     */
    public function acquireTemplate($template_id_short){
        $getUrl = "https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token=".$this->access_token;
        $params = array(
            'template_id_short' => $template_id_short,
        );
        $params = json_encode($params);
        $data = post_oneself($getUrl,$params);
        return $data;
    }

    /**
     * post请求前的准备
     * @param $arr
     * @param $url
     * $e: 0-不拼access_token 、1-要拼access_token
     * @return bool|mixed
     */
    public function parameterPostBtn($arr,$url,$e = 0){
        if(!is_array($arr)){
            return false;
        }
        if(!$url){
            return false;
        }
        if($e){
            $url = $url.$this->access_token;
        }
        $params = json_encode($arr);
        //dump($params);
        $data = post_oneself($url,$params);
        //dump($data);die;
        return $data;
    }

    /**
     * 获取微信用户信息
     * @param $access_token
     * @param $fromUserName
     * @return bool|mixed
     */
    public function weiXinUserName($fromUserName){
        //return $fromUserName;
        $getUrl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={$this->access_token}&openid={$fromUserName}&lang=zh_CN ";
        $data = get_oneself($getUrl);
        if(isset($data['errcode'])){
            return false;
        }
        return $data;
    }

    public function orderquery(){
        $appId = C('APPID');

    }
    /**
     * 菜单
     */
    public function caidan($params){
        //dump($this->access_token);
        $getUrl = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token='.$this->access_token;
        $params = json_encode($params);
        $params=preg_replace("#\\\u([0-9a-f]{4})#ie", "iconv('UCS-2BE', 'UTF-8', pack('H4', '\\1'))", $params);
        $data = post_oneself($getUrl,$params);
        return $data;
        //return $data['errcode'] == 40018 ? false : true;
    }




    public function transmitText($object, $content, $funcFlag = 0)
    {
        $textTpl = "<xml>
        <ToUserName><![CDATA[%s]]></ToUserName>
        <FromUserName><![CDATA[%s]]></FromUserName>
        <CreateTime>%s</CreateTime>
        <MsgType><![CDATA[text]]></MsgType>
        <Content><![CDATA[%s]]></Content>
        <FuncFlag>%d</FuncFlag>
        </xml>";
        $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $funcFlag);
        return $resultStr;
    }

   

}